package com.gc.client;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.gc.model.GcCommunicationModel;

public class GcClientConnection {
	public static GcClientConnection clientConnection;
	private Socket socket;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;

	private GcClientConnection() {
		try {
			String host = "203.233.194.231";
			socket = new Socket(host, 7777);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static GcClientConnection getInstance() {
		if (clientConnection == null)
			clientConnection = new GcClientConnection();

		return clientConnection;
	}

	public void write(GcCommunicationModel model) {
		try {
			oos = new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(model);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public GcCommunicationModel read() {
		GcCommunicationModel model = null;
		try {
			ois = new ObjectInputStream(socket.getInputStream());
			model = (GcCommunicationModel) ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return model;
	}

}
